package com.bw.home.repository

import android.util.Log
import androidx.room.Query
import com.bw.home.entity.VideoEntity
import com.bw.home.entity.VideoTypeEntity
import com.bw.home.entity.VideoTypeEntityItem
import com.bw.home.net.ApiService
import com.example.common.base.BaseRepo
import com.example.common.base.Res
import com.example.common.net.RetrofitFactory
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.flatMapConcat
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.onStart


class HomeRepository: BaseRepo() {
    private val apiService = RetrofitFactory.instance.create<ApiService>()

    /**
     * 获取视频类型列表
     * @return Flow<VideoTypeEntity> 视频类型数据流
     */
        fun getVideoType(): Flow<Res<VideoTypeEntity>> {
            return apiService.getVideoType()
                .onStart { Log.d("HomeRepository", "开始请求") }
                .onEach { data ->
                    Log.d("HomeRepository", "收到数据: ${data}条")
                }
                .catch { e ->
                    Log.e("HomeRepository", "请求失败", e)
                    throw e // 或 emit(VideoTypeEntity.empty())
                }
        }

    fun getVideoList(labelId:String,page: Int,size: Int): Flow<Res<VideoEntity>> {
        return apiService.getVideoList(labelId,page,size)
            .onStart { Log.d("HomeRepository", "开始请求") }
            .onEach { data ->
                Log.d("HomeRepository", "收到数据: ${data}条")
            }
            .catch { e ->
                Log.e("HomeRepository", "请求失败", e)
                throw e // 或 emit(VideoTypeEntity.empty())
            }
    }




}